﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace doris.Admin {
    public partial class SMTPConfig : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (Request.QueryString["id"] == null) {
                DetailsView1.ChangeMode(DetailsViewMode.Insert);
            }
        }
        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e) {
            if (e.NewMode == DetailsViewMode.Edit) {
                DetailsView1.ChangeMode(DetailsViewMode.Edit);
            } else if (e.NewMode == DetailsViewMode.ReadOnly) {
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            } else if (e.NewMode == DetailsViewMode.Insert) {
                /*                int id = Convert.ToInt32(((Label)DetailsView1.FindControl("IdLabel")).Text);
                                string name = ((TextBox)FormView1.FindControl("NameTextBox")).Text;
                                string city = ((TextBox)FormView1.FindControl("CityTextBox")).Text;
                                string state = ((TextBox)FormView1.FindControl("StateTextBox")).Text;
                                Employee.UpdateEmployee(id, name, city, state);*/
            }
        }
        protected void DetailsView1_ItemUpdating(object sender, SqlDataSourceCommandEventArgs e) {
            CheckBox cb = (CheckBox)DetailsView1.FindControl("cbUseCredentials");
            CheckBox cbS = (CheckBox)DetailsView1.FindControl("cbUseSSL");
            CheckBox cbE = (CheckBox)DetailsView1.FindControl("cbEnabled");

            // putt inn alle 
            e.Command.Parameters["@in_server"].Value = e.Command.Parameters["@Server"].Value;
            e.Command.Parameters["@in_configurationmailid"].Value = e.Command.Parameters["@ConfigurationMailID"].Value;
            e.Command.Parameters["@in_connectionname"].Value = e.Command.Parameters["@ConnectionName"].Value;
            e.Command.Parameters["@in_usecredentials"].Value = (cb.Checked ? "1" : "0");
            e.Command.Parameters["@in_username"].Value = e.Command.Parameters["@Username"].Value;
            e.Command.Parameters["@in_password"].Value = e.Command.Parameters["@Password"].Value;
            e.Command.Parameters["@in_port"].Value = e.Command.Parameters["@Port"].Value;
            e.Command.Parameters["@in_usessl"].Value = (cbS.Checked ? "1" : "0");
            e.Command.Parameters["@in_enabled"].Value = (cbE.Checked ? "1" : "0");

            e.Command.Parameters.Remove(e.Command.Parameters["@Server"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@ConfigurationMailID"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@ConnectionName"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@UseCredentials"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Username"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Password"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Port"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@UseSSL"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Enabled"]);
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e) {
            Response.Redirect("MailConfig.aspx");
        }

        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e) {
            CheckBox cb = (CheckBox)DetailsView1.FindControl("cbUseCredentials");
            CheckBox cbS = (CheckBox)DetailsView1.FindControl("cbUseSSL");
            CheckBox cbE = (CheckBox)DetailsView1.FindControl("cbEnabled");

            // putt inn alle 
            e.Command.Parameters["@in_server"].Value = e.Command.Parameters["@Server"].Value;
            e.Command.Parameters["@in_connectionname"].Value = e.Command.Parameters["@ConnectionName"].Value;
            e.Command.Parameters["@in_usecredentials"].Value = (cb.Checked ? "1" : "0");
            e.Command.Parameters["@in_username"].Value = e.Command.Parameters["@Username"].Value;
            e.Command.Parameters["@in_password"].Value = e.Command.Parameters["@Password"].Value;
            e.Command.Parameters["@in_port"].Value = e.Command.Parameters["@Port"].Value;
            e.Command.Parameters["@in_usessl"].Value = (cbS.Checked ? "1" : "0");
            e.Command.Parameters["@in_enabled"].Value = (cbE.Checked ? "1" : "0");

            e.Command.Parameters.Remove(e.Command.Parameters["@Server"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@ConnectionName"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Username"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Password"]);
            e.Command.Parameters.Remove(e.Command.Parameters["@Port"]);
 
        }
    }
}